﻿using System.Globalization;
using KinonekoSoftware.Foundation.UI.Media;

namespace KinonekoSoftware.Foundation.UI.Converters
{
#if AVALONIA

    using Avalonia.Data.Converters;
    
#else
    using System.Windows.Data;
    
#endif
    
    public class ColorToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is not Color c)
            {
                c = MediaFactory.ToColor(value?.ToString());
            }

            var b = MediaFactory.ToSolidColorBrush(c);
            b.Opacity = Opacity;
            return b;
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
        
        public double Opacity { get; set; }
    }
}